package cn.com.css.taxInspection.attention.service.impl;

import cn.com.css.taxInspection.attention.entity.AjcxCondition;
import cn.com.css.taxInspection.attention.entity.AjcxRecord;
import cn.com.css.taxInspection.attention.mapper.AjcxRecordMapper;
import cn.com.css.taxInspection.attention.service.AjcxRecordService;
import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.util.TsBqUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Slf4j
public class AjcxRecordServiceImpl extends ServiceImpl<AjcxRecordMapper, AjcxRecord> implements AjcxRecordService {

    @Resource
    private HttpSessionService httpSessionService;

    @Resource
    private AjcxRecordMapper ajcxRecordMapper;

    @Override
    public Page<AjcxRecord> getAjcxRecordPage(AjcxCondition req) throws ParseException {
        String currentSwjgDm = httpSessionService.getCurrentDeptNo();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (!StringUtils.isEmpty(req.getAjztDm())) {
            req.setAjztDms(Arrays.asList(req.getAjztDm().split(",")));
        }
        Page<AjcxRecord> page = new Page<>(req.getPage(), req.getLimit());
        LambdaQueryWrapper<AjcxRecord> queryWrapper = Wrappers.lambdaQuery();
        if (TsBqUtils.isNotNull(req.getBz())) {
            queryWrapper.like(AjcxRecord::getAjszjgDm, currentSwjgDm.substring(0, 8));
        } else {
            queryWrapper.eq(AjcxRecord::getAjszjgDm, currentSwjgDm.substring(0, 9) + "00");
        }
        if (!StringUtils.isEmpty(req.getNsrsbh())) {
            queryWrapper.like(AjcxRecord::getNsrsbh, req.getNsrsbh());
        }
        if (!StringUtils.isEmpty(req.getNsrmc())) {
            queryWrapper.like(AjcxRecord::getNsrmc, req.getNsrmc());
        }
        if (!StringUtils.isEmpty(req.getAjbh())) {
            queryWrapper.like(AjcxRecord::getJcajbh, req.getAjbh());
        }
        //立案开始时间
        if (!StringUtils.isEmpty(req.getLastartTime())) {
            queryWrapper.ge(AjcxRecord::getLarq, sdf.parse(req.getLastartTime()));
            queryWrapper.le(AjcxRecord::getLarq, sdf.parse(req.getLaendTime()));
        }
        //执行完毕时间
        if (!StringUtils.isEmpty(req.getZxwbstartTime())) {
            queryWrapper.ge(AjcxRecord::getZxwbrq, sdf.parse(req.getZxwbstartTime()));
            queryWrapper.le(AjcxRecord::getZxwbrq, sdf.parse(req.getZxwbendTime()));
        }
        //结案时间
        if (!StringUtils.isEmpty(req.getJastartTime())) {
            queryWrapper.ge(AjcxRecord::getJarq, sdf.parse(req.getJastartTime()));
            queryWrapper.le(AjcxRecord::getJarq, sdf.parse(req.getJaendTime()));
        }
        if (!TsBqUtils.isNull(req.getAjztDms())) {
            if (req.getAjztDm().contains("901")) {
                queryWrapper.eq(AjcxRecord::getAjjcztDm, "901");
            }
            for (int i = 0; i < req.getAjztDms().size(); i++) {
                String tmpajztdm = req.getAjztDms().get(i);
                if (req.getAjztDm().contains("901")) {
                    if ("901".equals(tmpajztdm)) {
                        continue;
                    }
                    queryWrapper.or().likeRight(AjcxRecord::getAjjcztDm, tmpajztdm);
                } else {
                    if (i == 0) {
                        queryWrapper.likeRight(AjcxRecord::getAjjcztDm, tmpajztdm);
                    } else {
                        queryWrapper.or().likeRight(AjcxRecord::getAjjcztDm, tmpajztdm);
                    }
                }
            }
        }
        //所在科室
        if (TsBqUtils.isNotNull(req.getJcbmDm())) {
            queryWrapper.eq(AjcxRecord::getJcbmDm, req.getJcbmDm());
        }
        //多选
        if (TsBqUtils.isNotNull(req.getJcbmDmList())) {
            queryWrapper.in(AjcxRecord::getJcbmDm, req.getJcbmDmList());
        }
        queryWrapper.orderByDesc(AjcxRecord::getJcajbh);
        return this.page(page, queryWrapper);
    }

    @Override
    //导出案件查询信息
    public List<AjcxRecord> exportAjcxRecord(AjcxCondition req) throws ParseException {
        String currentSwjgDm = httpSessionService.getCurrentDeptNo();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (!StringUtils.isEmpty(req.getAjztDm())) {
            req.setAjztDms(Arrays.asList(req.getAjztDm().split(",")));
        }
        LambdaQueryWrapper<AjcxRecord> queryWrapper = Wrappers.lambdaQuery();
        if (TsBqUtils.isNotNull(req.getBz())) {
            queryWrapper.like(AjcxRecord::getAjszjgDm, currentSwjgDm.substring(0, 8));
        } else {
            queryWrapper.eq(AjcxRecord::getAjszjgDm, currentSwjgDm.substring(0, 9) + "00");
        }
        if (!StringUtils.isEmpty(req.getNsrsbh())) {
            queryWrapper.like(AjcxRecord::getNsrsbh, req.getNsrsbh());
        }
        if (!StringUtils.isEmpty(req.getNsrmc())) {
            queryWrapper.like(AjcxRecord::getNsrmc, req.getNsrmc());
        }
        if (!StringUtils.isEmpty(req.getAjbh())) {
            queryWrapper.like(AjcxRecord::getJcajbh, req.getAjbh());
        }
        //立案开始时间
        if (!StringUtils.isEmpty(req.getLastartTime())) {
            queryWrapper.ge(AjcxRecord::getLarq, sdf.parse(req.getLastartTime()));
            queryWrapper.le(AjcxRecord::getLarq, sdf.parse(req.getLaendTime()));
        }
        //执行完毕时间
        if (!StringUtils.isEmpty(req.getZxwbstartTime())) {
            queryWrapper.ge(AjcxRecord::getZxwbrq, sdf.parse(req.getZxwbstartTime()));
            queryWrapper.le(AjcxRecord::getZxwbrq, sdf.parse(req.getZxwbendTime()));
        }
        //结案时间
        if (!StringUtils.isEmpty(req.getJastartTime())) {
            queryWrapper.ge(AjcxRecord::getJarq, sdf.parse(req.getJastartTime()));
            queryWrapper.le(AjcxRecord::getJarq, sdf.parse(req.getJaendTime()));
        }
        if (!TsBqUtils.isNull(req.getAjztDms())) {
            if (req.getAjztDm().contains("901")) {
                queryWrapper.eq(AjcxRecord::getAjjcztDm, "901");
            }
            for (int i = 0; i < req.getAjztDms().size(); i++) {
                String tmpajztdm = req.getAjztDms().get(i);
                if (req.getAjztDm().contains("901")) {
                    if ("901".equals(tmpajztdm)) {
                        continue;
                    }
                    queryWrapper.or().likeRight(AjcxRecord::getAjjcztDm, tmpajztdm);
                } else {
                    if (i == 0) {
                        queryWrapper.likeRight(AjcxRecord::getAjjcztDm, tmpajztdm);
                    } else {
                        queryWrapper.or().likeRight(AjcxRecord::getAjjcztDm, tmpajztdm);
                    }
                }
            }
        }
        //所在科室
        if (TsBqUtils.isNotNull(req.getJcbmDm())) {
            queryWrapper.eq(AjcxRecord::getJcbmDm, req.getJcbmDm());
        }
        //多选
        if (TsBqUtils.isNotNull(req.getJcbmDmList())) {
            queryWrapper.in(AjcxRecord::getJcbmDm, req.getJcbmDmList());
        }
        queryWrapper.isNotNull(AjcxRecord::getLarq);
        queryWrapper.orderByDesc(AjcxRecord::getLarq);
        return this.list(queryWrapper);
    }

    @Override
    public Page getClerkIndexBjajPage(AjcxCondition req) throws ParseException {
        Page page = new Page(req.getPage(), req.getLimit());
        String currentUserId = httpSessionService.getCurrentUserId();
        LambdaQueryWrapper<AjcxRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AjcxRecord::getAjjcztDm, "901")
                .like(AjcxRecord::getJcry, currentUserId)
                .orderByDesc(AjcxRecord::getLarq);
        Page resPage = this.page(page, queryWrapper);
        return resPage;
    }
//需求：检查人个人首页
    // 科长审批案件数（本身份在本年本系统在科长岗审批的案件），
    // 主管局长审批案件数（本身份在本年本系统在检查主管局长岗审批的案件），
    // 本年助手报审案件数，

    //在查案件数（金三中在查数 减掉中止检查） 装填2开头-205，
    // 本年提审案件数量（金三送审数）审理登记日期在本年内的案件，
    // 补充稽查案件数（金三）案件状态为204，
    // 本年审结案件数（出两书、稽查结论或终结检查），（出两书之前查过，hx_jc.jc_swjcjl 用jcajxxuuid关联，终结检查502、终结执行503)  ，
    // 中止检查205，
    // 办结案件901
    //中止检查案件数 金三
    //助手报审案件数
    @Override
    public Map<String, Object> getClerkIndexAjcountXzs() {
        String currentUserId = httpSessionService.getCurrentUserId();
        return ajcxRecordMapper.getClerkIndexAjcountXzs(currentUserId);
    }

    @Override
    public Map<String, Object> getClerkIndexAjcountGt3() {
        String currentUserId = httpSessionService.getCurrentUserId();
        String currentDeptId = httpSessionService.getCurrentDeptId();
        return ajcxRecordMapper.getClerkIndexAjcountGt3(currentUserId, currentDeptId);
    }

    @Override
    public Map<String, Object> initRightTenCaseCount() {
        String currentRoleIds = httpSessionService.getCurrentRolesId();
        String currentSwjgDm = httpSessionService.getCurrentDeptNo();
        String dataScope = TsBqUtils.getDataScopeByRolesId(currentRoleIds);
        /**
         * jcj=当前登录人所在稽查局各个科室数据
         * all=各个稽查局的数据
         */
        return ajcxRecordMapper.initRightTenCaseCount(dataScope, currentSwjgDm.substring(0, 9));
    }

    @Override
    public Map<String, Object> initTopJajzTips() {
        String currentRoleIds = httpSessionService.getCurrentRolesId();
        String currentSwjgDm = httpSessionService.getCurrentDeptNo();
        String dataScope = TsBqUtils.getDataScopeByRolesId(currentRoleIds);
        /**
         * jcj=当前登录人所在稽查局各个科室数据
         * all=各个稽查局的数据
         */
        Map<String, Object> resMap = ajcxRecordMapper.initTopJajzTips(dataScope, currentSwjgDm.substring(0, 9));
        resMap.put("dataScope", dataScope);
        return resMap;
    }

    @Override
    public AjcxRecord getAjcxRecordByJcajbh(String jcajbh) {
        LambdaQueryWrapper<AjcxRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AjcxRecord::getJcajbh, jcajbh);
        return this.getOne(queryWrapper);
    }


    @Override
    public List<AjcxRecord> jascajcx(AjcxCondition req) throws ParseException {
        LambdaQueryWrapper<AjcxRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.likeRight(AjcxRecord::getAjszjgDm, "12101");
        if (!TsBqUtils.isNull(req.getAjztDms())) {
            if (req.getAjztDm().indexOf("901") != -1) {
                queryWrapper.eq(AjcxRecord::getAjjcztDm, "901");
            }
            for (int i = 0; i < req.getAjztDms().size(); i++) {
                String tmpajztdm = req.getAjztDms().get(i);
                if (req.getAjztDm().indexOf("901") != -1) {
                    if ("901".equals(tmpajztdm)) {
                        continue;
                    }
                    queryWrapper.or().likeRight(AjcxRecord::getAjjcztDm, tmpajztdm);
                } else {
                    if (i == 0) {
                        queryWrapper.likeRight(AjcxRecord::getAjjcztDm, tmpajztdm);
                    } else {
                        queryWrapper.or().likeRight(AjcxRecord::getAjjcztDm, tmpajztdm);
                    }
                }
            }
        }
        return this.list(queryWrapper);
    }
}
